package com.nondo.dean.product.controller.api.inner;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.nondo.dean.product.model.inner.ProductBomBeans;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.nondo.dean.common.annotation.ApiVersion;
import com.nondo.dean.common.web.View;
import com.nondo.dean.product.domain.ProductBom;
import com.nondo.dean.product.model.ProductBomBean;
import com.nondo.dean.product.service.ProductBomService;

/**
 * @author xiaofan
 * @Description: ${todo}
 * @throws
 * @date 2018-03-3016:27
 */
@RestController("apiInnerProductBom")
@RequestMapping("/{v}/inner/product/bom")
public class ProductBomController {

    @Autowired
    ProductBomService productBomService;

    /**
     * 接收sap返回的bom数据
     * @return
     */
    @ApiVersion(1)
    @RequestMapping("/sapSynBom")
    public int synProductBom(@RequestBody String result){
        Object jsonObject=JSON.parse(result);
        String jsonString = jsonObject.toString();

        List<ProductBomBeans> productBomList=JSONObject.parseArray(jsonString, ProductBomBeans.class);
       return productBomService.synProductBom(productBomList);

    }

    /**
     * 根据父级编码查询商品BOM列表
     *
     * @param request
     * @param response
     * @param productCode 产品编码
     * @return 商品BOM详细信息 {@link ProductBom}
     */
    @ApiVersion(1)
    @RequestMapping("/list/{productCode}")
    public View<List<ProductBomBean>> list(HttpServletRequest request, HttpServletResponse response, @PathVariable String productCode) {
        View<List<ProductBomBean>> view = new View<List<ProductBomBean>>(20000, "获取数据成功");

        List<ProductBomBean> productBomList = this.productBomService.selectByParentProductCode(productCode);
        
        view.setData(productBomList);

        return view;
    }

}
